local daigong = fk.CreateSkill{
  name = "iberia__daigong",
}

local prompts = {}
prompts.await_exhausted = "iberia__daigong-use"
prompts.mark = "@@iberia__daigong"

Fk:loadTranslationTable{
  ["iberia__daigong"] = "怠攻",
  [":iberia__daigong"] = "攻击范围包含你的角色的出牌阶段开始时，你可以将一张牌当【以逸待劳】使用，" ..
    "令其拥有〖崩坏〗直到回合结束时或对你造成伤害后。",

  [prompts.await_exhausted] = "怠攻：可以将一张牌当【以逸待劳】使用，令%src拥有〖崩坏〗直到回合结束或对你造成伤害时。",
  [prompts.mark] = "怠攻",
}

daigong:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(daigong.name) and target:inMyAttackRange(player) and not target:hasSkill("iberia__benghuai", true) then
      return table.find(player:getCardIds("he"), function (id)
        local await_exhausted = Fk:cloneCard("iberia__await_exhausted")
        await_exhausted.skillName = daigong.name
        await_exhausted:addSubcard(id)
        return player:canUse(await_exhausted)
      end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local use = player.room:askToUseVirtualCard(player, {
      name = "iberia__await_exhausted",
      skill_name = daigong.name,
      card_filter = { n = 1 }, skip = true,
      prompt = prompts.await_exhausted .. ":" .. target.id,
    })

    if use then
      event:setCostData(self, use)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local use = event:getCostData(self)
    local room = player.room
    room:useCard(use)

    room:handleAddLoseSkills(target, "iberia__benghuai")
    room:setPlayerMark(target, prompts.mark, player.id)
  end
})

daigong:addEffect(fk.Damage, {
  can_refresh = function (self, event, target, player, data)
    return player == target and player:getMark(prompts.mark) == data.to.id
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, prompts.mark, 0)
    room:handleAddLoseSkills(target, "-iberia__benghuai")
  end
})

daigong:addEffect(fk.TurnEnd, {
  can_refresh = function (self, event, target, player, data)
    return player:getMark(prompts.mark) ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, prompts.mark, 0)
    room:handleAddLoseSkills(target, "-iberia__benghuai")
  end
})

return daigong